package cc.rengu.redp.bizimpl.upmp.controller;

import cc.rengu.redp.common.annotation.Log;
import cc.rengu.redp.common.controller.BaseController;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import cc.rengu.redp.bizimpl.upmp.domain.NuccInstInfo;
import cc.rengu.redp.bizimpl.upmp.service.NuccInstInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.wuwenze.poi.ExcelKit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 * @author LiuYuLiang.LL
 */
@Slf4j
@Validated
@Api(tags = "参数管理:网联机构号管理")
@RestController
@RequestMapping("/admin/merParam/nuccInstInfo")
public class NuccInstInfoController extends BaseController {
    @Autowired
    private NuccInstInfoService nuccInstInfoService;

    @GetMapping
//    @RequiresPermissions("areaInfo:view")
    public Map<String, Object> list(QueryRequest request, NuccInstInfo entity) {
        return getDataTable(this.nuccInstInfoService.findByPage(request, entity));
    }

    @Log("查询单条")
    @GetMapping(value = "/getOne")
    @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    public NuccInstInfo getOne(@NotBlank(message = "{required}") String id) {
        return nuccInstInfoService.getById(id);
    }

    @Log("新增")
    @PostMapping
//    @RequiresPermissions("areaInfo:add")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "nuccInstId", value = "网联机构代码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "nuccInstName", value = "网联机构名称", required = true, dataType = "String")
    })
    public void add(@Valid NuccInstInfo entity) throws RedpException {
        try {
            LambdaQueryWrapper<NuccInstInfo> wrapper = new LambdaQueryWrapper();
            wrapper.eq(NuccInstInfo::getNuccInstId, entity.getNuccInstId());
            NuccInstInfo info = nuccInstInfoService.getOne(wrapper);
            if (null != info) {
                throw new RedpException("[ 网联机构代码 ] 重复，请重新输入");
            }
            wrapper = new LambdaQueryWrapper();
            wrapper.eq(NuccInstInfo::getNuccInstName, entity.getNuccInstName());
            info = nuccInstInfoService.getOne(wrapper);
            if (null != info) {
                throw new RedpException("[ 网联机构名称 ] 重复，请重新输入");
            }
            //设置备付金账户默认值
            if (StringUtils.isBlank(entity.getInstgAcctId())) {
                entity.setInstgAcctId("*");
            }
            this.nuccInstInfoService.create(entity);
        } catch (Exception e) {
            String message = "新增失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("修改")
    @PutMapping("/update")
//    @RequiresPermissions("areaInfo:update")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    })
    public void update(@Valid NuccInstInfo entity) throws RedpException {
        try {
            LambdaQueryWrapper<NuccInstInfo> wrapper = new LambdaQueryWrapper();
            wrapper.eq(NuccInstInfo::getNuccInstId, entity.getNuccInstId());
            wrapper.ne(NuccInstInfo::getId, entity.getId());
            NuccInstInfo info = nuccInstInfoService.getOne(wrapper);
            if (null != info) {
                throw new RedpException("[ 网联机构代码 ] 重复，请重新输入");
            }
            wrapper = new LambdaQueryWrapper();
            wrapper.ne(NuccInstInfo::getId, entity.getId());
            wrapper.eq(NuccInstInfo::getNuccInstName, entity.getNuccInstName());
            info = nuccInstInfoService.getOne(wrapper);
            if (null != info) {
                throw new RedpException("[ 网联机构名称 ] 重复，请重新输入");
            }
            this.nuccInstInfoService.update(entity);
        } catch (Exception e) {
            String message = "修改失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("注销")
    @PutMapping("/cancel")
//    @RequiresPermissions("areaInfo:cancel")
    @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    public void cancel(@NotBlank(message = "{required}") String id) throws RedpException {
        try {
            this.nuccInstInfoService.cancel(id);
        } catch (Exception e) {
            String message = "注销失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("删除")
    @DeleteMapping("/{ids}")
//    @RequiresPermissions("areaInfo:delete")
    @ApiImplicitParam(paramType = "path", name = "ids", value = "id主键，逗号隔开", required = true, dataType = "String")
    public void delete(@NotBlank(message = "{required}") @PathVariable String ids) throws RedpException {
        try {
            this.nuccInstInfoService.delete(ids.split(StringPool.COMMA));
        } catch (Exception e) {
            String message = "删除失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @PostMapping("excel")
//    @RequiresPermissions("dict:export")
    public void export(QueryRequest request, NuccInstInfo entity, HttpServletResponse response) throws RedpException {
        try {
            List<NuccInstInfo> list = this.nuccInstInfoService.findByPage(request, entity).getRecords();
            ExcelKit.$Export(NuccInstInfo.class, response).downXlsx(list, false);
        } catch (Exception e) {
            String message = "导出Excel失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @GetMapping(value = "/select2")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "q", value = "网联机构号|网联机构名称", dataType = "String"),
    })
    public Map<String, Object> select2(String q, QueryRequest request) {

        return nuccInstInfoService.select2(q, request);

    }
}
